package com.example.shiyan8;
import java. io. File;

import android.Manifest;
import android. app. Activity;
import android.content.pm.PackageManager;
import android. graphics. PixelFormat;
import android. os. Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android. view. View;
import android. view. View. OnClickListener;
import android. widget. Button;
import android. widget. EditText;
import android. widget. MediaController;
import android. widget. Toast;
import android. widget. VideoView;
public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    VideoView videoView;
    private String fielname;
    MediaController mediaController;
    EditText editText;
    Button openButton;
    Button qiutButton;
    Button playButton;
    Button pauseButton;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super. onCreate(savedInstanceState) ;

        getWindow().setFormat(PixelFormat.TRANSLUCENT) ;
        setContentView(R.layout.activity_main) ;
        videoView=findViewById(R.id.video) ;
        editText= findViewById(R.id.edittext) ;
        openButton=findViewById(R.id.starttoplay) ;
        openButton.setOnClickListener(new startlistener() ) ;
        qiutButton=findViewById(R.id.quit) ;
        qiutButton.setOnClickListener(new qiutlistener() ) ;
        playButton=findViewById(R.id.play);
        playButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                videoView.start();
            }
        });
        pauseButton=findViewById(R.id.pause);
        pauseButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                videoView.pause();
            }
        });


        mediaController=new MediaController(this) ;
    }
    class startlistener implements OnClickListener{
        @Override
        public void onClick(View v)
        {
// TODO Auto-generated method stub
            fielname=editText. getText() . toString();
            System. out. println(fielname) ;
            File video=new File(fielname) ;
//若文件被找到
            if(video. exists() ) {
                Toast. makeText(MainActivity. this,video. getAbsolutePath() , Toast. LENGTH_LONG) . show() ;
                //安卓6.0后要动态申请权限
                if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {//检查是否有了权限
                    if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
                    } else {
                        //没有权限即动态申请
                        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
                    }
                }


                videoView. setVideoPath(video. getAbsolutePath() ) ; // 文件绝对路径
                videoView. setMediaController(mediaController) ; //设 置videoView与mediaControler 的关联
                mediaController. setMediaPlayer(videoView) ;
               // videoView.start();
                videoView. requestFocus() ;
            }
            else{
                Toast. makeText(MainActivity. this, "文件不存在", Toast. LENGTH_LONG) . show() ;
            }
        }
    }
    class qiutlistener implements OnClickListener{
        @Override
        public void onClick(View v)
        {
// TODO Auto-generated method stub
            finish() ;
        }
    }
}
